package com.pearadmin.modules.sys.controller.manage;

import com.github.pagehelper.PageInfo;
import com.pearadmin.common.constant.ControllerConstant;
import com.pearadmin.common.tools.SecurityUtil;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.modules.sys.domain.SlotInfo;
import com.pearadmin.modules.sys.domain.SysDept;
import com.pearadmin.modules.sys.domain.SysUser;
import com.pearadmin.modules.sys.domain.dto.SlotInfoDto;
import com.pearadmin.modules.sys.service.ISlotInfoService;
import com.pearadmin.modules.sys.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;

/**
 * 槽号信息Controller
 *
 * @author Mr.Ling
 * @date 2021-11-27
 */
@RestController
@Api(tags = {"槽号信息"})
@RequestMapping(ControllerConstant.API_MANAGE_PREFIX + "slotInfo")
public class SlotInfoController extends BaseController {
    private String prefix = "manage/slotInfo";

    @Autowired
    private ISlotInfoService slotInfoService;

    @Autowired
    private ISysDeptService sysDeptService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/manage/slotInfo/main','manage:slotInfo:main')")
    public ModelAndView main(Model model) {
        SysDept dept = new SysDept();
        dept.setDeptCode("elec_workshop");//电解车间
        model.addAttribute("depts", sysDeptService.list(dept));
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询槽号信息列表
     */
    @ResponseBody
    @ApiOperation("列表")
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/manage/slotInfo/data','manage:slotInfo:data')")
    public ResultTable list(@ModelAttribute SlotInfo slotInfo, PageDomain pageDomain) {
        PageInfo<SlotInfo> pageInfo = slotInfoService.selectSlotInfoPage(slotInfo, pageDomain);
        //前端渲染
        //for (SlotInfo info : pageInfo.getList()) {
        //    SysDept dept = sysDeptService.getById(info.getDeptId());
        //    info.setDeptId(dept.getDeptName());
        //}
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 通过部门id查询槽号信息列表
     */
    @ResponseBody
    @GetMapping("/dataByDeptId")
    public Result dataByDeptId(String deptId) {
        SlotInfo slotInfo = new SlotInfo();
        slotInfo.setDeptId(deptId);
        return Result.success(slotInfoService.selectSlotInfoList(slotInfo));
    }

    /**
     * 新增槽号信息
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/manage/slotInfo/add','manage:slotInfo:add')")
    public ModelAndView add(Model model) {
        SysDept dept = new SysDept();
        dept.setDeptCode("elec_workshop");
        model.addAttribute("depts", sysDeptService.list(dept));
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存槽号信息
     */
    @ResponseBody
    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/manage/slotInfo/add','manage:slotInfo:add')")
    public Result save(@RequestBody SlotInfoDto slotInfo) {
        //前端校验为数字
        if (slotInfo.getStartSlotNo()!=null && slotInfo.getEndSlotNo()!=null) {
            if (slotInfo.getStartSlotNo() > slotInfo.getEndSlotNo()) {
                return Result.failure("开始槽号不能小于结束槽号");
            }
        }
        if (slotInfo.getDeptId()!=null) {
            SysDept dept = sysDeptService.getById(slotInfo.getDeptId());
            slotInfo.setDeptName(dept.getDeptName());
        }
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        slotInfo.setCreateTime(LocalDateTime.now());
        slotInfo.setCreateBy(sysUser.getUserId());
        slotInfo.setCreateName(sysUser.getRealName());
        return decide(slotInfoService.insertSlotInfo(slotInfo));
    }

    /**
     * 修改槽号信息
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/manage/slotInfo/edit','manage:slotInfo:edit')")
    public ModelAndView edit(String id, ModelMap mmap) {
        SlotInfo slotInfo = slotInfoService.selectSlotInfoById(id);
        slotInfo.setUpdateTime(LocalDateTime.now());
        mmap.put("slotInfo", slotInfo);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存槽号信息
     */
    @ResponseBody
    @ApiOperation("更新")
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/manage/slotInfo/edit','manage:slotInfo:edit')")
    public Result update(@RequestBody SlotInfo slotInfo) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        slotInfo.setUpdateTime(LocalDateTime.now());
        slotInfo.setUpdateBy(sysUser.getUserId());
        slotInfo.setUpdateName(sysUser.getUsername());
        return decide(slotInfoService.updateSlotInfo(slotInfo));
    }

    /**
     * 删除槽号信息
     */
    @ResponseBody
    @ApiOperation("批量刪除")
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/manage/slotInfo/remove','manage:slotInfo:remove')")
    public Result batchRemove(String ids) {
        return decide(slotInfoService.deleteSlotInfoByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @ApiOperation("根据id 刪除")
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/manage/slotInfo/remove','manage:slotInfo:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(slotInfoService.deleteSlotInfoById(id));
    }
}
